// 自己实现简单版的迭代器 Iterators
let chef = ( foods ) => {
    let i = 0;
    return {
        next(){
            let done = ( i >= foods.length);
            let value = !done ? foods[i++] : undefined;
            return{
                value,
                done
            }
        }
    }
}

let anghao = chef(['tomato','egg']);
console.log( anghao.next() );
console.log( anghao.next() );
console.log( anghao.next() );

// es 6 的Generators 

function* es6_chef( foods ) {
    for (let i = 0; i < foods.length; i++) {
        yield foods[i];
    }
}

//第二种
let wanghao = chef(['tomato','egg']);
console.log(wanghao.next()); // {value: "tomato", done: false}
console.log(wanghao.next()); // {value: "egg", done: false}
console.log(wanghao.next()); // {value: undefined, done: true}